package com.fc.data;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.fc.control.MemberControl;
import com.fc.model.Member;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class Export {

	//导出member表中的数据
	@SuppressWarnings("unchecked")
	public static void ExportMember(String filename)throws IOException{
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("啥玩意儿");
		sheet.setColumnWidth(0, 3500);//单元格大小
		
		HSSFCellStyle normalStyle = wb.createCellStyle();
		normalStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		normalStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		normalStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		normalStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		//normalStyle.setWrapText(true);//是否自動換行
		
		//int colIndex = 1;//控制单元格的位置；
		//int colSize = 0;//控制合并单元格的长度
		
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = null;
		
		cell = row.createCell(0);
		cell.setCellValue("店铺编号");
		cell.setCellStyle(normalStyle);
		
		cell = row.createCell(1);
		cell.setCellValue("开始时间");
		cell.setCellStyle(normalStyle);
		
		cell = row.createCell(2);
		cell.setCellValue("会员类型");
		cell.setCellStyle(normalStyle);
		
		cell = row.createCell(3);
		cell.setCellValue("锻炼次数");
		cell.setCellStyle(normalStyle);
		
		cell = row.createCell(4);
		cell.setCellValue("结束时间");
		cell.setCellStyle(normalStyle);
		
		cell = row.createCell(5);
		cell.setCellValue("状态");
		cell.setCellStyle(normalStyle);
		
		cell = row.createCell(6);
		cell.setCellValue("电话");
		cell.setCellStyle(normalStyle);
		
		cell = row.createCell(7);
		cell.setCellValue("性别");
		cell.setCellStyle(normalStyle);
		
		cell = row.createCell(8);
		cell.setCellValue("身份证号");
		cell.setCellStyle(normalStyle);
		
		cell = row.createCell(9);
		cell.setCellValue("姓名");
		cell.setCellStyle(normalStyle);
		
		cell = row.createCell(10);
		cell.setCellValue("用户名");
		cell.setCellStyle(normalStyle);
		
		Collection members = MemberControl.getAllMembers();
		Iterator membersIterator = members.iterator();
		int index = 1;
		while(membersIterator.hasNext()){
			Member member = (Member) membersIterator.next();
			row = sheet.createRow(index++);
			//System.out.println(member.getStore_id());
		 cell = row.createCell(0);
         cell.setCellValue(member.getStore_id());
         
	     cell.setCellStyle(normalStyle);
	
	     cell = row.createCell(1);
	     cell.setCellValue(member.getStart_time());
	     cell.setCellStyle(normalStyle);
	
	     cell = row.createCell(2);
	     cell.setCellValue(member.getKind());
	     cell.setCellStyle(normalStyle);
	
	     cell = row.createCell(3);
	     cell.setCellValue(member.getDegree());
	     cell.setCellStyle(normalStyle);
	
	     cell = row.createCell(4);
	     cell.setCellValue(member.getEnd_time());
	     cell.setCellStyle(normalStyle);
	
	     cell = row.createCell(5);
	     cell.setCellValue(member.getState());
	     cell.setCellStyle(normalStyle);
	
	     cell = row.createCell(6);
	     cell.setCellValue(member.getPhone());
	     cell.setCellStyle(normalStyle);
	     
	     cell = row.createCell(7);
	     cell.setCellValue(member.getSex());
	     cell.setCellStyle(normalStyle);
	     
	     cell = row.createCell(8);
	     cell.setCellValue(member.getIdcard());
	     cell.setCellStyle(normalStyle);
	     
	     cell = row.createCell(9);
	     cell.setCellValue(member.getName());
	     cell.setCellStyle(normalStyle);
	     
	     cell = row.createCell(10);
	     cell.setCellValue(member.getUsername());
	     cell.setCellStyle(normalStyle);
 	}
	FileOutputStream out = new FileOutputStream(filename);
	wb.write(out);
	out.close();
	}
	public static void main(String[] args) throws IOException,ParseException{
		Export.ExportMember("C:\\member.xls");
	}
}
